<?

/***************************************************************************
 Jupiter Content System @ Jupiterportal.com
 Copyright (C) 2005 Cosmin Flavius (highstrike@gmail.com)

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 More Info About The Licence At http://www.gnu.org/copyleft/gpl.html
****************************************************************************/

//-----------------------------------------------------------------
//  Filter Acces Rightes
//-----------------------------------------------------------------

if(isset($is_guest) || isset($is_user) || isset($is_moderator))
{ header("location: $PHP_SELF?i=2"); exit; }

switch($a)
{
	//-----------------------------------------------------------------
	//  Categories Managment
	//-----------------------------------------------------------------

	case 1:

		//-----------------------------------------------------------------
		//  Start second switch
		//-----------------------------------------------------------------

		switch($c)
		{
			//-----------------------------------------------------------------
			//  Add New Category
			//-----------------------------------------------------------------

			case 1:

				//-----------------------------------------------------------------
				//  Begin Check
				//-----------------------------------------------------------------

				if(!$name){ header("location: $PHP_SELF?n=modules/forum-admin&a=1&i=113"); exit; }
				$name = jcm_stripslashes($name);

				//-----------------------------------------------------------------
				//  Make sure it adds +1 in order after adding
				//-----------------------------------------------------------------

				$cats_db = $db->getTable("SELECT * FROM forum_cat ORDER by 'order' ASC");

				foreach($cats_db as $idx=>$cat_id)
					$last_cat_order = $cat_id['order'];

				$cat_order = $last_cat_order + 1;

				//-----------------------------------------------------------------
				//  Add the cat
				//-----------------------------------------------------------------

				$db->insertRow("forum_cat",array('name' => ''.$name.'','order' => ''.$cat_order.''));

				//-----------------------------------------------------------------
				//  Write Log 
				//-----------------------------------------------------------------

				$db->insertRow("log",array('user_id' => ''.$_SESSION['id'].'','user_user' => ''.$_SESSION['username'].'','log' => ''.$language['Logs desc3'].' <b>forum</b> the category -> \''.$name.'\'','date' => ''.time().''));

				//-----------------------------------------------------------------
				//  Redirect when finished
				//-----------------------------------------------------------------

				header("location: $PHP_SELF?n=modules/forum-admin&a=1&i=114");

			break;

			//-----------------------------------------------------------------
			//  Edit Category
			//-----------------------------------------------------------------

			case 2:

				//-----------------------------------------------------------------
				//  Begin Check
				//-----------------------------------------------------------------

				foreach($name as $idx=>$val) if(!$name[$idx]){ header("location: $PHP_SELF?n=modules/forum-admin&a=1&i=115"); exit; }

				//-----------------------------------------------------------------
				//  Save the Links
				//-----------------------------------------------------------------

				foreach($order as $idx=>$val)
				{
					//-----------------------------------------------------------------
					//  Get Original category
					//-----------------------------------------------------------------

					$forum_cat = $db->getLine("SELECT * FROM forum_cat",$idx);

					//-----------------------------------------------------------------
					//  Write Log
					//-----------------------------------------------------------------

					if($forum_cat['order'] != $val)
						$db->insertRow("log",array('user_id' => ''.$_SESSION['id'].'','user_user' => ''.$_SESSION['username'].'','log' => ''.$language['Logs desc2'].' <b>forum</b> the category -> \''.$forum_cat['name'].'\' to order \''.$val.'\'','date' => ''.time().''));

					//-----------------------------------------------------------------
					//  Write in DB
					//-----------------------------------------------------------------

					$db->query("Update `forum_cat` set `order` = '$val' WHERE `id` = '$idx'");
				}

				foreach($name as $idx=>$val)
				{
					//-----------------------------------------------------------------
					//  Get Original category
					//-----------------------------------------------------------------

					$forum_cat = $db->getLine("SELECT * FROM forum_cat",$idx);

					//-----------------------------------------------------------------
					//  Write Log
					//-----------------------------------------------------------------

					if($forum_cat['name'] != $val)
						$db->insertRow("log",array('user_id' => ''.$_SESSION['id'].'','user_user' => ''.$_SESSION['username'].'','log' => ''.$language['Logs desc2'].' <b>forum</b> the category\'s name -> \''.$forum_cat['name'].'\' to \''.$val.'\'','date' => ''.time().''));

					//-----------------------------------------------------------------
					//  Write in DB
					//-----------------------------------------------------------------

					$db->query("Update `forum_cat` set `name` = '$val' WHERE `id` = '$idx'");
				}

				//-----------------------------------------------------------------
				//  Redirect when finished
				//-----------------------------------------------------------------

				header("location: $PHP_SELF?n=modules/forum-admin&a=1&i=116");

			break;

			//-----------------------------------------------------------------
			//  Delete Category
			//-----------------------------------------------------------------

			case 3:

				//-----------------------------------------------------------------
				//  Delete the Link
				//-----------------------------------------------------------------

				$forum_replies = $db->getTable("SELECT * FROM forum_replies WHERE cat = '$d'");
				$forum_topics = $db->getTable("SELECT * FROM forum_topics WHERE cat = '$d'");
				$forum_subcats = $db->getTable("SELECT * FROM forum_subcat WHERE cat = '$d'");
				$forum_cat = $db->getLine("SELECT * FROM forum_cat WHERE id = '$d'");

				foreach($forum_replies as $forum_reply) $db->deleteRow("forum_replies","cat = '$d'");
				foreach($forum_topics as $forum_topic) $db->deleteRow("forum_topics","cat = '$d'");
				foreach($forum_subcats as $forum_subcat) $db->deleteRow("forum_subcat","cat = '$d'");

				$db->deleteRow("forum_cat","id = '$d'");

				//-----------------------------------------------------------------
				//  Write Log
				//-----------------------------------------------------------------

				$db->insertRow("log",array('user_id' => ''.$_SESSION['id'].'','user_user' => ''.$_SESSION['username'].'','log' => ''.$language['Logs desc'].' <b>forum</b> the category -> \''.$forum_cat['name'].'\'','date' => ''.time().''));

				//-----------------------------------------------------------------
				//  Redirect when finished
				//-----------------------------------------------------------------

				header("location: $PHP_SELF?n=modules/forum-admin&a=1&i=117");

			break;

			//-----------------------------------------------------------------
			//  View Categories Managment Form
			//-----------------------------------------------------------------

			default:

				$categories = $db->getTable("SELECT * FROM forum_cat ORDER by 'order' ASC");

				if($categories != FALSE)
				{
					foreach($categories as $idx=>$category)
					{
						//-----------------------------------------------------------------
						//  Make Layout
						//-----------------------------------------------------------------

						$layout .= "
							<tr class='".row2color($idx)."' onMouseOver=this.className='con3' onMouseOut=this.className='".row2color($idx)."'>
							<td width='5%'><input type='text' name='order[{$category['id']}]' value='{$category['order']}' maxlength='3' class='box' style='width:100%'></td>
							<td width='90%'><input type='text' name='name[{$category['id']}]' value='{$category['name']}' maxlength='50' class='box' style='width:100%'></td>
							<td width='5%'><a href='$PHP_SELF?n=modules/forum-admin&a=1&c=3&d={$category['id']}'><img src='templates/$template/images/buttons/delete.gif' border='0' alt='{$language['Forum view admin desc11']}'></a></td>
							</tr>";
					}
				}

				//-----------------------------------------------------------------
				//  Echo the back link
				//-----------------------------------------------------------------

				echo "<table class='main' cellspacing='1' cellpadding='4'><tr class='head'><td colspan='3'>{$language['Admin name']}</td></tr><tr><td colspan='3' class='con1' onMouseOver=this.className='con3' onMouseOut=this.className='con1' onclick=window.location.href='$PHP_SELF?n=modules/admin'><a href='$PHP_SELF?n=modules/admin'> &raquo; {$language['Admin back']}</a></td></tr></table><br>";

				?>
					<form method="post" action="<?= $PHP_SELF ?>?n=modules/forum-admin">
					<table class="main" cellspacing="1" cellpadding="4">
					<tr class="head"><td colspan="4"><?= $language['Forum view admin desc2'] ?></td></tr>
					<tr class="bottom"><td width="75%"><?= $language['Forum view admin desc3'] ?></td><td width="25%"><?= $language['Forum view admin desc4'] ?></td></tr>
					<tr class="con1"><td><input type='text' name='name' class='box' style='width:100%'></td><td><input type='submit' name='submit' style='width:100%' class='box' value='<?= $language['Forum view admin desc5'] ?>'></td></tr>
					<input type="hidden" name="a" value="1">
					<input type="hidden" name="c" value="1">
					</table></form>
					<br>
				<?

				if($categories != FALSE)
				{
					?>
						<form method="post" action="<?= $PHP_SELF ?>?n=modules/forum-admin">
						<table class="main" cellspacing="1" cellpadding="4">
						<tr class="head"><td colspan="5"><?= $language['Forum view admin desc6'] ?></td></tr>
						<tr class="bottom"><td><?= $language['Forum view admin desc7'] ?></td><td colspan="2"><?= $language['Forum view admin desc8'] ?></td></tr>
						<?= $layout ?>
						</table>
						<br>
						<table class="main" cellspacing="1" cellpadding="4">
						<tr class='con1'>
						<td width="50%"><?= $language['Forum view admin desc9'] ?></td>
						<td width="50%" align="right"><input type='submit' name='submit' style='width:50%' class='box' value='<?= $language['Forum view admin desc10'] ?>'></td>
						</tr>
						<input type="hidden" name="a" value="1">
						<input type="hidden" name="c" value="2">
						</table></form>
					<?
				}
				else
				{
					?>
						<table class='main' cellspacing='1' cellpadding='4'>
						<tr class='con1'>
						<td colspan='3'><?= $language['Forum view admin desc'] ?></td>
						</tr></table>
					<?
				}
		}

	break;

	//-----------------------------------------------------------------
	//  Subcategories Managment
	//-----------------------------------------------------------------

	case 2:

		//-----------------------------------------------------------------
		//  Start second switch
		//-----------------------------------------------------------------

		switch($c)
		{
			//-----------------------------------------------------------------
			//  Add New Subcategory
			//-----------------------------------------------------------------

			case 1:

				//-----------------------------------------------------------------
				//  Get Original
				//-----------------------------------------------------------------

				$forum_cat = $db->getLine("SELECT * FROM forum_cat WHERE id = '$cat'");

				//-----------------------------------------------------------------
				//  Begin Check
				//-----------------------------------------------------------------

				if(!$name){ header("location: $PHP_SELF?n=modules/forum-admin&a=2&i=118"); exit; }
				$name = jcm_stripslashes($name);

				//-----------------------------------------------------------------
				//  Make sure it adds +1 in order after adding
				//-----------------------------------------------------------------

				$subcats_db = $db->getTable("SELECT * FROM forum_subcat WHERE cat = '$cat' ORDER by 'order' ASC");

				foreach($subcats_db as $idx=>$subcat_id)
					$last_subcat_order = $subcat_id['order'];

				$subcat_order = $last_subcat_order + 1;

				//-----------------------------------------------------------------
				//  Add the cat
				//-----------------------------------------------------------------

				$db->insertRow("forum_subcat",array('cat' => ''.$cat.'','name' => ''.$name.'','order' => ''.$subcat_order.''));

				//-----------------------------------------------------------------
				//  Write Log
				//-----------------------------------------------------------------

				$db->insertRow("log",array('user_id' => ''.$_SESSION['id'].'','user_user' => ''.$_SESSION['username'].'','log' => ''.$language['Logs desc3'].' <b>forum</b> the subcategory -> \''.$name.'\' to \''.$forum_cat['name'].'\'','date' => ''.time().''));

				//-----------------------------------------------------------------
				//  Redirect when finished
				//-----------------------------------------------------------------

				header("location: $PHP_SELF?n=modules/forum-admin&a=2&i=119");

			break;

			//-----------------------------------------------------------------
			//  Save Subcategories Order
			//-----------------------------------------------------------------

			case 2:

				//-----------------------------------------------------------------
				//  Save the Links
				//-----------------------------------------------------------------

				foreach($order as $idx=>$val)
				{
					//-----------------------------------------------------------------
					//  Get Original category
					//-----------------------------------------------------------------

					$forum_subcat = $db->getLine("SELECT * FROM forum_subcat",$idx);

					//-----------------------------------------------------------------
					//  Write Log
					//-----------------------------------------------------------------

					if($forum_subcat['order'] != $val)
						$db->insertRow("log",array('user_id' => ''.$_SESSION['id'].'','user_user' => ''.$_SESSION['username'].'','log' => ''.$language['Logs desc2'].' <b>forum</b> the subcategory -> \''.$forum_subcat['name'].'\' to order \''.$val.'\'','date' => ''.time().''));

					//-----------------------------------------------------------------
					//  Write in DB
					//-----------------------------------------------------------------

					$db->query("Update `forum_subcat` set `order` = '$val' WHERE `id` = '$idx'");
				}

				//-----------------------------------------------------------------
				//  Redirect when finished
				//-----------------------------------------------------------------

				header("location: $PHP_SELF?n=modules/forum-admin&a=2&i=120");

			break;

			//-----------------------------------------------------------------
			//  Edit Subcategory
			//-----------------------------------------------------------------

			case 3:

				//-----------------------------------------------------------------
				//  Load Database
				//-----------------------------------------------------------------

				$cats = $db->getTable("SELECT * FROM forum_cat");
				$subcat = $db->getLine("SELECT * FROM forum_subcat WHERE id = '$d'");

				//-----------------------------------------------------------------
				//  Start Edit Nav
				//-----------------------------------------------------------------

				switch($k)
				{
					//-----------------------------------------------------------------
					//  Do the Edit
					//-----------------------------------------------------------------

					case 1:

						//-----------------------------------------------------------------
						//  Get Original category and subcategory
						//-----------------------------------------------------------------

						$forum_cat = $db->getLine("SELECT * FROM forum_cat",$cat);
						$forum_subcat = $db->getLine("SELECT * FROM forum_subcat",$subcat['id']);

						//-----------------------------------------------------------------
						//  Start Checks
						//-----------------------------------------------------------------

						if(!$name){ header("location: $PHP_SELF?n=modules/forum-admin&a=2&c=3&d=$d&i=121"); exit; }
						if($subcat['type'] == 5) { if(!$redirect_location){ header("location: $PHP_SELF?n=modules/forum-admin&a=2&c=3&d=$d&i=122"); exit; }}
						$name = jcm_stripslashes($name);
						$description = jcm_stripslashes($description);

						//-----------------------------------------------------------------
						//  Write in Database
						//-----------------------------------------------------------------

						$tmp['name'] = $name;
						$tmp['cat'] = $cat;
						$tmp['type'] = $type;
						$tmp['description'] = $description;
						if($subcat['type'] == 5) { $tmp['redirect_location'] = $redirect_location; }

						$db->updateRow("forum_subcat",$tmp,"id={$subcat['id']}");

						//-----------------------------------------------------------------
						//  Write Log
						//-----------------------------------------------------------------

						if($type == 1) $type_text = "Open";
						if($type == 2) $type_text = "News";
						if($type == 3) $type_text = "Admin";
						if($type == 4) $type_text = "Locked";
						if($type == 5) $type_text = "Redirected";

						if($forum_subcat['name'] != $name)
							$db->insertRow("log",array('user_id' => ''.$_SESSION['id'].'','user_user' => ''.$_SESSION['username'].'','log' => ''.$language['Logs desc2'].' <b>forum</b> the subcategory\'s name -> \''.$forum_subcat['name'].'\' to \''.$name.'\'','date' => ''.time().''));

						if($forum_subcat['cat'] != $cat)
							$db->insertRow("log",array('user_id' => ''.$_SESSION['id'].'','user_user' => ''.$_SESSION['username'].'','log' => ''.$language['Logs desc2'].' <b>forum</b> the subcategory\'s cat -> \''.$forum_subcat['name'].'\' moved to \''.$forum_cat['name'].'\'','date' => ''.time().''));

						if($forum_subcat['type'] != $type)
							$db->insertRow("log",array('user_id' => ''.$_SESSION['id'].'','user_user' => ''.$_SESSION['username'].'','log' => ''.$language['Logs desc2'].' <b>forum</b> the subcategory\'s type -> \''.$forum_subcat['name'].'\' to \''.$type_text.'\'','date' => ''.time().''));

						if($subcat['type'] == 5)
							if($forum_subcat['redirect_location'] != $redirect_location)
								$db->insertRow("log",array('user_id' => ''.$_SESSION['id'].'','user_user' => ''.$_SESSION['username'].'','log' => ''.$language['Logs desc2'].' <b>forum</b> the subcategory\'s redirect location -> \''.$forum_subcat['name'].'\' to \''.$redirect_location.'\'','date' => ''.time().''));

						if($forum_subcat['description'] != $description)
							$db->insertRow("log",array('user_id' => ''.$_SESSION['id'].'','user_user' => ''.$_SESSION['username'].'','log' => ''.$language['Logs desc2'].' <b>forum</b> the subcategory\'s description -> \''.$forum_subcat['name'].'\'','date' => ''.time().''));

						//-----------------------------------------------------------------
						//  Redirect when finished
						//-----------------------------------------------------------------

						header("location: $PHP_SELF?n=modules/forum-admin&a=2&c=3&d=$d&i=123");

					break;

					//-----------------------------------------------------------------
					//  View Edit Form
					//-----------------------------------------------------------------

					default:

						//-----------------------------------------------------------------
						//  Echo the back link
						//-----------------------------------------------------------------

						echo "<table class='main' cellspacing='1' cellpadding='4'><tr class='head'><td colspan='3'>{$language['Forum view edit desc']}</td></tr><tr><td colspan='3' class='con1' onMouseOver=this.className='con3' onMouseOut=this.className='con1' onclick=window.location.href='$PHP_SELF?n=modules/forum-admin&a=2'><a href='$PHP_SELF?n=modules/forum-admin&a=2'> &raquo; {$language['Forum view edit desc2']}</a></td></tr></table><br>";
						
						?>
							<form method="post" action="<?= $PHP_SELF ?>?n=modules/forum-admin">
							<table class="main" cellspacing="1" cellpadding="4">
							<col width="25%"><col width="75%">
							<tr class="head"><td colspan="2"><?= $language['Forum view edit desc3'] ?> [<?= $subcat['name'] ?>]</td></tr>
							<tr class="con1"><td><?= $language['Forum view edit desc4'] ?></td><td><input type='text' name='name' class='box' style='width:50%' value='<?= $subcat['name'] ?>'></td></tr>
							<tr class="con1"><td><?= $language['Forum view edit desc5'] ?></td><td><select name="cat" class="box" style="width:50%"><? foreach($cats as $cat) { if($cat['id'] == $subcat['cat']) $selected = "selected"; else $selected = NULL; echo "<option value='{$cat['id']}' $selected>{$cat['name']}</option>"; } ?></select></td></tr>
							<tr class="con1"><td><?= $language['Forum view edit desc6'] ?></td><td><select name="type" class="box" style="width:50%"><? if($subcat['type'] == 1) { echo "<option value='1' selected>{$language['Forum view edit desc11']}</option><option value='2'>{$language['Forum view edit desc7']}</option><option value='3'>{$language['Forum view edit desc8']}</option><option value='4'>{$language['Forum view edit desc9']}</option><option value='5'>{$language['Forum view edit desc10']}</option>"; } if($subcat['type'] == 2) { echo "<option value='1'>{$language['Forum view edit desc11']}</option><option value='2' selected>{$language['Forum view edit desc7']}</option><option value='3'>{$language['Forum view edit desc8']}</option><option value='4'>{$language['Forum view edit desc9']}</option><option value='5'>{$language['Forum view edit desc10']}</option>"; } if($subcat['type'] == 3) { echo "<option value='1'>{$language['Forum view edit desc11']}</option><option value='2'>{$language['Forum view edit desc7']}</option><option value='3' selected>{$language['Forum view edit desc8']}</option><option value='4'>{$language['Forum view edit desc9']}</option><option value='5'>{$language['Forum view edit desc10']}</option>"; }if($subcat['type'] == 4) { echo "<option value='1'>{$language['Forum view edit desc11']}</option><option value='2'>{$language['Forum view edit desc7']}</option><option value='3'>{$language['Forum view edit desc8']}</option><option value='4' selected>{$language['Forum view edit desc9']}</option><option value='5'>{$language['Forum view edit desc10']}</option>"; } if($subcat['type'] == 5) { echo "<option value='1'>{$language['Forum view edit desc11']}</option><option value='2'>{$language['Forum view edit desc7']}</option><option value='3'>{$language['Forum view edit desc8']}</option><option value='4'>{$language['Forum view edit desc9']}</option><option value='5' selected>{$language['Forum view edit desc10']}</option>"; } ?></select></td></tr>
							<? if($subcat['type'] == 5) { ?><tr class="con1"><td><?= $language['Forum view edit desc12'] ?></td><td><input type='text' name='redirect_location' class='box' style='width:50%' value='<?= $subcat['redirect_location'] ?>'></td></tr><? } ?>
							<tr class="con1"><td valign="top"><?= $language['Forum view edit desc13'] ?></td><td><textarea style="width:80%; height:105px;" class="box" name="description"><?= $subcat['description'] ?></textarea></td></tr>
							<tr class="con1"><td><?= $language['Forum view edit desc14'] ?></td><td><input type="submit" style="width:120" class="box" value="<?= $language['Forum view edit desc15'] ?>"></td></tr>
							<input type="hidden" name="a" value="2">
							<input type="hidden" name="c" value="3">
							<input type="hidden" name="k" value="1">
							<input type="hidden" name="d" value="<?= $d ?>">
							</table></form>
						<?
				}

			break;

			//-----------------------------------------------------------------
			//  Delete Subcategory
			//-----------------------------------------------------------------

			case 4:

				//-----------------------------------------------------------------
				//  Get Original subcategory
				//-----------------------------------------------------------------

				$forum_subcat = $db->getLine("SELECT * FROM forum_subcat",$d);

				//-----------------------------------------------------------------
				//  Delete the Link
				//-----------------------------------------------------------------

				$forum_replies = $db->getTable("SELECT * FROM forum_replies WHERE subcat = '$d'");
				$forum_topics = $db->getTable("SELECT * FROM forum_topics WHERE subcat = '$d'");

				foreach($forum_replies as $forum_reply) $db->deleteRow("forum_replies","subcat = '$d'");
				foreach($forum_topics as $forum_topic) $db->deleteRow("forum_topics","subcat = '$d'");

				$db->deleteRow("forum_subcat","id = '$d'");

				//-----------------------------------------------------------------
				//  Write Log
				//-----------------------------------------------------------------

				$db->insertRow("log",array('user_id' => ''.$_SESSION['id'].'','user_user' => ''.$_SESSION['username'].'','log' => ''.$language['Logs desc'].' <b>forum</b> the subcategory -> \''.$forum_subcat['name'].'\'','date' => ''.time().''));

				//-----------------------------------------------------------------
				//  Redirect when finished
				//-----------------------------------------------------------------

				header("location: $PHP_SELF?n=modules/forum-admin&a=2&i=124");

			break;

			//-----------------------------------------------------------------
			//  View Subcategories Managment Form
			//-----------------------------------------------------------------

			default:

				//-----------------------------------------------------------------
				//  Code for Subcats
				//-----------------------------------------------------------------

				$categories = $db->getTable("SELECT c.id AS cid, c.order AS corder, c.name AS cname, s.id AS sid, s.order AS sorder, s.name AS sname FROM forum_cat AS c LEFT JOIN forum_subcat AS s ON s.cat=c.id ORDER by c.order ASC, s.order ASC");
				$cur_category = 0;
				$cat_count = 0;

				if($categories != FALSE)
				{
					foreach($categories as $idx=>$cat)
					{
						if($cat['cid'] != $cur_category)
						{
							if ($cur_category != 0) $layout .= "</table><br>";

							$cat_count++;

							//-----------------------------------------------------------------
							//  Make Layout
							//-----------------------------------------------------------------

							$layout .= "
								<table class='main' cellspacing='1' cellpadding='4'>
								<tr class='head'><td colspan='3'>{$cat['cname']}</td></tr>
								<tr class='bottom'><td width='5%'>{$language['Forum view subcat desc']}</td><td colspan='2' width='95%'>{$language['Forum view subcat desc2']}</td></tr>";

							$cur_category = $cat['cid'];
						}

						if($cat['sid'] != FALSE)
						{
							//-----------------------------------------------------------------
							//  Make Layout
							//-----------------------------------------------------------------

							$layout .= "
								<tr class='".row2color($idx)."' onMouseOver=this.className='con3' onMouseOut=this.className='".row2color($idx)."'>
								<td><input type='text' name='order[{$cat['sid']}]' value='{$cat['sorder']}' maxlength='2' class='box' style='width:100%'></td>
								<td>{$cat['sname']}</td>
								<td width='8%'><a href='$PHP_SELF?n=modules/forum-admin&a=2&c=3&d={$cat['sid']}'><img src='templates/$template/images/buttons/edit.gif' border='0' alt='{$language['Forum view subcat desc13']}'></a><a href='$PHP_SELF?n=modules/forum-admin&a=2&c=4&d={$cat['sid']}'><img src='templates/$template/images/buttons/delete.gif' border='0' alt='{$language['Forum view subcat desc14']}'></a></td></tr>";
						}
						else $layout .= "<tr class='con1'><td colspan='3'>{$language['Forum view subcat desc3']}</td></tr>";
					}

					if($cur_category > 0) $layout .= "</table><br>";
				}

				//-----------------------------------------------------------------
				//  Echo the back link
				//-----------------------------------------------------------------

				echo "<table class='main' cellspacing='1' cellpadding='4'><tr class='head'><td colspan='3'>{$language['Admin name']}</td></tr><tr><td colspan='3' class='con1' onMouseOver=this.className='con3' onMouseOut=this.className='con1' onclick=window.location.href='$PHP_SELF?n=modules/admin'><a href='$PHP_SELF?n=modules/admin'> &raquo; {$language['Admin back']}</a></td></tr></table><br>";
				
				//-----------------------------------------------------------------
				//  View Form
				//-----------------------------------------------------------------
				
				if($categories != FALSE)
				{ 
					?>
						<form method="post" action="<?= $PHP_SELF ?>?n=modules/forum-admin">
						<table class="main" cellspacing="1" cellpadding="4">
						<tr class="head"><td colspan="4"><?= $language['Forum view subcat desc5'] ?></td></tr>
						<tr class="bottom"><td width="50%"><?= $language['Forum view subcat desc6'] ?></td><td width="25%"><?= $language['Forum view subcat desc7'] ?></td><td width="25%"><?= $language['Forum view subcat desc8'] ?></td></tr>
						<tr class="con1"><td><input type='text' name='name' class='box' style='width:100%'></td><td><select name="cat" class="box" style="width:100%"><? foreach($categories as $cat) $categories_unique[$cat['cname']]=$cat; foreach($categories_unique as $cat) echo "<option value='{$cat['cid']}'>{$cat['cname']}</option>" ?></select></td><td><input type='submit' name='submit' style='width:100%' class='box' value='<?= $language['Forum view subcat desc9'] ?>'></td></tr>
						<input type="hidden" name="a" value="2">
						<input type="hidden" name="c" value="1">
						</table></form>
						<br>
						<form method="post" action="<?= $PHP_SELF ?>?n=modules/forum-admin">
						<?= $layout ?>
						<table class="main" cellspacing="1" cellpadding="4">
						<tr class='con1'>
						<td width="50%"><?= $language['Forum view subcat desc10'] ?></td>
						<td width="50%" align="right"><input type='submit' name='submit' style='width:50%' class='box' value='<?= $language['Forum view subcat desc11'] ?>' tabindex='3'></td>
						</tr>
						<input type="hidden" name="a" value="2">
						<input type="hidden" name="c" value="2">
						</table>
					<?
				}
				else
				{
					?>
						<table class='main' cellspacing='1' cellpadding='4'>
						<tr class='con1'>
						<td colspan='3'><?= $language['Forum view subcat desc4'] ?></td>
						</tr></table>
					<?
				}
		}

	break;

	//-----------------------------------------------------------------
	//  Prune Managment
	//-----------------------------------------------------------------

	case 3:

		//-----------------------------------------------------------------
		//  Start second switch
		//-----------------------------------------------------------------

		switch($c)
		{			
			//-----------------------------------------------------------------
			//  Do Prune
			//-----------------------------------------------------------------
			
			case 1:

				//-----------------------------------------------------------------
				//  Begin Check
				//-----------------------------------------------------------------

				if(!$prune_days){ header("location: $PHP_SELF?n=modules/forum-admin&a=3&i=125"); exit; }

				//-----------------------------------------------------------------
				//  Calculate time
				//-----------------------------------------------------------------

				$prune_date = time() - ( $prune_days * 86400 );

				//-----------------------------------------------------------------
				//  See if we have to do all or just one subcat
				//-----------------------------------------------------------------

				if($subcats == "all")
				{
					//-----------------------------------------------------------------
					//  Count all topics and Delete topics and the posts from Database
					//-----------------------------------------------------------------

					$forum_topics = $db->getTable("SELECT id FROM forum_topics WHERE date < '$prune_date'");
					foreach($forum_topics as $forum_topic)
					{
						$forum_replies = $db->getTable("SELECT id FROM forum_replies WHERE topic = '{$forum_topic['id']}'");

						foreach($forum_replies as $forum_reply)
							$db->deleteRow("forum_replies","id = '{$forum_reply['id']}'");

						$db->deleteRow("forum_topics","id = '{$forum_topic['id']}'");
					}

					//-----------------------------------------------------------------
					//  Take care of subcats information
					//-----------------------------------------------------------------

					$forum_subcats = $db->getTable("SELECT id FROM forum_subcat");

					foreach($forum_subcats as $forum_subcat)
					{
						$forum_topics_after = $db->getInfo_countRows("forum_topics","subcat = '{$forum_subcat['id']}' AND type != '3'");

						if($forum_topics_after == 0)
						{
							$db->updateRow("forum_subcat",array('lastpost_location' => NULL, 'lastpost_date' => NULL, 'lastpost_reg' => NULL, 'lastpost_id' => NULL, 'lastpost_user' => NULL, 'lastpost_email' => NULL),"id = '{$forum_subcat['id']}'");
						}
						else
						{
							$forum_topics_intact = $db->getLine("SELECT * FROM forum_topics WHERE subcat = '{$forum_subcat['id']}' ORDER BY 'id' DESC LIMIT 1");

							$last_info['lastpost_location'] = $forum_topics_intact['lastpost_location'];
							$last_info['lastpost_date'] = $forum_topics_intact['lastpost_date'];
							$last_info['lastpost_reg'] = $forum_topics_intact['lastpost_reg'];
							$last_info['lastpost_id'] = $forum_topics_intact['lastpost_id'];
							$last_info['lastpost_user'] = $forum_topics_intact['lastpost_user'];
							$last_info['lastpost_email'] = $forum_topics_intact['lastpost_email'];

							$db->updateRow("forum_subcat",$last_info,"id = '{$forum_subcat['id']}'");
						}

						$check_replies = $db->getInfo_countRows("forum_replies","subcat = '{$forum_subcat['id']}'");
						$db->updateRow("forum_subcat",array('topics' => ''.$forum_topics_after.'', 'replies' => ''.$check_replies.''),"id = '{$forum_subcat['id']}'");
					}

					//-----------------------------------------------------------------
					//  Write Log
					//-----------------------------------------------------------------

					$db->insertRow("log",array('user_id' => ''.$_SESSION['id'].'','user_user' => ''.$_SESSION['username'].'','log' => ''.$language['Logs desc2'].' <b>forum</b> prune -> \'all subcategories\' since '.today_yesterday($prune_date).'','date' => ''.time().''));
				}
				else
				{
					//-----------------------------------------------------------------
					//  Count all topics and Delete topics and the posts from Database
					//-----------------------------------------------------------------

					$forum_topics = $db->getTable("SELECT id FROM forum_topics WHERE subcat = '$subcats' AND date < '$prune_date'");
					foreach($forum_topics as $forum_topic)
					{
						$forum_replies = $db->getTable("SELECT id FROM forum_replies WHERE subcat = '$subcats' AND topic = '{$forum_topic['id']}'");

						foreach($forum_replies as $forum_reply)
							$db->deleteRow("forum_replies","subcat = '$subcats' AND id = '{$forum_reply['id']}'");

						$db->deleteRow("forum_topics","subcat = '$subcats' AND id = '{$forum_topic['id']}'");
					}

					//-----------------------------------------------------------------
					//  Take care of subcats information
					//-----------------------------------------------------------------

					$forum_topics_after = $db->getInfo_countRows("forum_topics","subcat = '$subcats' AND type != '3'");

					if($forum_topics_after == 0)
					{
						$db->updateRow("forum_subcat",array('lastpost_location' => NULL, 'lastpost_date' => NULL, 'lastpost_reg' => NULL, 'lastpost_id' => NULL, 'lastpost_user' => NULL, 'lastpost_email' => NULL),"id = '$subcats'");
					}
					else
					{
						$forum_topics_intact = $db->getLine("SELECT * FROM forum_topics WHERE subcat = '$subcats' ORDER BY 'id' DESC LIMIT 1");

						$last_info['lastpost_location'] = $forum_topics_intact['lastpost_location'];
						$last_info['lastpost_date'] = $forum_topics_intact['lastpost_date'];
						$last_info['lastpost_reg'] = $forum_topics_intact['lastpost_reg'];
						$last_info['lastpost_id'] = $forum_topics_intact['lastpost_id'];
						$last_info['lastpost_user'] = $forum_topics_intact['lastpost_user'];
						$last_info['lastpost_email'] = $forum_topics_intact['lastpost_email'];

						$db->updateRow("forum_subcat",$last_info,"id = '$subcats'");
					}

					$check_replies = $db->getInfo_countRows("forum_replies","subcat = '$subcats'");
					$db->updateRow("forum_subcat",array('topics' => ''.$forum_topics_after.'', 'replies' => ''.$check_replies.''),"id = '$subcats'");

					//-----------------------------------------------------------------
					//  Write Log
					//-----------------------------------------------------------------

					$forum_subcat = $db->getLine("SELECT name FROM forum_subcat WHERE id = '$subcats'");

					$db->insertRow("log",array('user_id' => ''.$_SESSION['id'].'','user_user' => ''.$_SESSION['username'].'','log' => ''.$language['Logs desc2'].' <b>forum</b> prune -> \''.$forum_subcat['name'].'\' since '.today_yesterday($prune_date).'','date' => ''.time().''));
				}

				//-----------------------------------------------------------------
				//  Redirect when finished
				//-----------------------------------------------------------------

				header("location: $PHP_SELF?n=modules/forum-admin&a=3&i=126");

			break;
			
			//-----------------------------------------------------------------
			//  View Prune Form
			//-----------------------------------------------------------------
			
			default:

				//-----------------------------------------------------------------
				//  Load Database
				//-----------------------------------------------------------------

				$forum_cats = $db->getTable("SELECT * FROM forum_cat ORDER by 'order' ASC");

				//-----------------------------------------------------------------
				//  Echo the back link
				//-----------------------------------------------------------------

				echo "<table class='main' cellspacing='1' cellpadding='4'><tr class='head'><td colspan='3'>{$language['Admin name']}</td></tr><tr><td colspan='3' class='con1' onMouseOver=this.className='con3' onMouseOut=this.className='con1' onclick=window.location.href='$PHP_SELF?n=modules/admin'><a href='$PHP_SELF?n=modules/admin'> &raquo; {$language['Admin back']}</a></td></tr></table><br>";

				//-----------------------------------------------------------------
				//  Display Layout
				//-----------------------------------------------------------------

				?>
					<form method="post" action="<?= $PHP_SELF ?>?n=modules/forum-admin">
					<table class="main" cellspacing="1" cellpadding="4">
					<col width="25%"><col width="75%">
					<tr class="head"><td colspan="2"><?= $language['Forum view prune desc'] ?></td></tr>
					<tr class="con1"><td colspan="2"><table width='100%' class='main' cellspacing='1' border='0' cellpadding='2'><tr><td width='1%'><img src='templates/<?= $template ?>/images/misc/note.jpg'></td><td class='empty'><?= $language['Forum view prune desc2'] ?><br><?= $language['Forum view prune desc3'] ?></td></tr></table></td></tr>
					<tr class="con1"><td><?= $language['Forum view prune desc4'] ?></td><td><input type='text' name='prune_days' maxlength="3" class='box' style='width:50%'></td></tr>
					<tr class="con1"><td><?= $language['Forum view prune desc5'] ?></td><td><select style="width:50%" class="box" name="subcats"><option value='all'><?= $language['Forum view prune desc6'] ?></option><? foreach($forum_cats as $idx=>$forum_cat){ $forum_subcats_move = $db->getTable("SELECT * FROM forum_subcat WHERE cat = '{$forum_cat['id']}' ORDER BY 'order' ASC"); echo "<optgroup label='{$forum_cat['name']}'>"; foreach($forum_subcats_move as $idx=>$forum_subcat_move) if($forum_subcat_move['type'] != 5) echo "<option value='{$forum_subcat_move['id']}'>{$forum_subcat_move['name']}</option>"; echo "</optgroup>";} ?></select></td></tr>
					<tr class="con1"><td><?= $language['Forum view prune desc7'] ?></td><td><input type="submit" style="width:120" class="box" value="<?= $language['Forum view prune desc8'] ?>"></td></tr>
					<input type="hidden" name="a" value="3">
					<input type="hidden" name="c" value="1">
					</table></form>
				<?
		}

	break;

	//-----------------------------------------------------------------
	//  Rank Managment
	//-----------------------------------------------------------------

	case 4:

		//-----------------------------------------------------------------
		//  Start second switch
		//-----------------------------------------------------------------

		switch($c)
		{
			//-----------------------------------------------------------------
			//  Add Rank
			//-----------------------------------------------------------------

			case 1:

				//-----------------------------------------------------------------
				//  Begin Check
				//-----------------------------------------------------------------

				if(!$name){ header("location: $PHP_SELF?n=modules/forum-admin&a=4&i=127"); exit; }
				if(!$posts){ header("location: $PHP_SELF?n=modules/forum-admin&a=4&i=128"); exit; }
				$name = jcm_stripslashes($name);

				//-----------------------------------------------------------------
				//  Add the rank
				//-----------------------------------------------------------------

				$db->insertRow("forum_ranks",array('name' => ''.$name.'','posts' => ''.$posts.''));

				//-----------------------------------------------------------------
				//  Write Log
				//-----------------------------------------------------------------

				$db->insertRow("log",array('user_id' => ''.$_SESSION['id'].'','user_user' => ''.$_SESSION['username'].'','log' => ''.$language['Logs desc3'].' <b>forum</b> the rank -> \''.$name.'\' at \''.$posts.'\' posts','date' => ''.time().''));

				//-----------------------------------------------------------------
				//  Redirect when finished
				//-----------------------------------------------------------------

				header("location: $PHP_SELF?n=modules/forum-admin&a=4&i=129");

			break;

			//-----------------------------------------------------------------
			//  Delete Rank
			//-----------------------------------------------------------------

			case 2:

				//-----------------------------------------------------------------
				//  Get Original rank
				//-----------------------------------------------------------------

				$forum_rank = $db->getLine("SELECT * FROM forum_ranks",$d);

				//-----------------------------------------------------------------
				//  Delete the rank
				//-----------------------------------------------------------------

				$db->deleteRow("forum_ranks","id = '$d'");

				//-----------------------------------------------------------------
				//  Write Log
				//-----------------------------------------------------------------

				$db->insertRow("log",array('user_id' => ''.$_SESSION['id'].'','user_user' => ''.$_SESSION['username'].'','log' => ''.$language['Logs desc'].' <b>forum</b> the rank -> \''.$forum_rank['name'].'\'','date' => ''.time().''));

				//-----------------------------------------------------------------
				//  Redirect when finished
				//-----------------------------------------------------------------

				header("location: $PHP_SELF?n=modules/forum-admin&a=4&i=130");

			break;

			//-----------------------------------------------------------------
			//  Update Ranks
			//-----------------------------------------------------------------

			case 3:

				//-----------------------------------------------------------------
				//  Begin Check
				//-----------------------------------------------------------------

				foreach($name as $idx=>$val) if(!$name[$idx]){ header("location: $PHP_SELF?n=modules/forum-admin&a=4&i=131"); exit; }
				foreach($posts as $idx=>$val) if(!$posts[$idx]){ header("location: $PHP_SELF?n=modules/forum-admin&a=4&i=132"); exit; }

				//-----------------------------------------------------------------
				//  Save the ranks
				//-----------------------------------------------------------------
	
				foreach($name as $idx=>$val)
				{
					//-----------------------------------------------------------------
					//  Get Original rank
					//-----------------------------------------------------------------

					$forum_rank = $db->getLine("SELECT * FROM forum_ranks",$idx);

					//-----------------------------------------------------------------
					//  Write Log
					//-----------------------------------------------------------------

					if($forum_rank['name'] != $val)
						$db->insertRow("log",array('user_id' => ''.$_SESSION['id'].'','user_user' => ''.$_SESSION['username'].'','log' => ''.$language['Logs desc2'].' <b>forum</b> the rank\'s name -> \''.$forum_rank['name'].'\' to \''.$val.'\'','date' => ''.time().''));

					//-----------------------------------------------------------------
					//  Write in DB
					//-----------------------------------------------------------------

					$db->query("Update `forum_ranks` set `name` = '$val' WHERE `id` = '$idx'");
				}

				foreach($posts as $idx=>$val)
				{
					//-----------------------------------------------------------------
					//  Get Original rank
					//-----------------------------------------------------------------

					$forum_rank = $db->getLine("SELECT * FROM forum_ranks",$idx);

					//-----------------------------------------------------------------
					//  Write Log
					//-----------------------------------------------------------------

					if($forum_rank['posts'] != $val)
						$db->insertRow("log",array('user_id' => ''.$_SESSION['id'].'','user_user' => ''.$_SESSION['username'].'','log' => ''.$language['Logs desc2'].' <b>forum</b> the rank\'s posts -> \''.$forum_rank['name'].'\' to \''.$val.'\'','date' => ''.time().''));

					//-----------------------------------------------------------------
					//  Write in DB
					//-----------------------------------------------------------------

					$db->query("Update `forum_ranks` set `posts` = '$val' WHERE `id` = '$idx'");
				}

				//-----------------------------------------------------------------
				//  Redirect when finished
				//-----------------------------------------------------------------

				header("location: $PHP_SELF?n=modules/forum-admin&a=4&i=133");

			break;

			//-----------------------------------------------------------------
			//  View Ranks Form
			//-----------------------------------------------------------------

			default:

				$ranks_db = $db->getTable("SELECT * FROM forum_ranks ORDER BY 'posts' ASC");

				if($ranks_db != FALSE)
				{
					foreach($ranks_db as $idx=>$rank)
					{
						//-----------------------------------------------------------------
						//  Make Layout
						//-----------------------------------------------------------------

						$layout .= "
							<tr class='".row2color($idx)."' onMouseOver=this.className='con3' onMouseOut=this.className='".row2color($idx)."'>
							<td width='62%'><input type='text' name='name[{$rank['id']}]' value='{$rank['name']}' class='box' style='width:100%'></td>
							<td width='33%'><input type='text' name='posts[{$rank['id']}]' maxlength='16' value='{$rank['posts']}' maxlength='50' class='box' style='width:100%'></td>
							<td width='5%'><a href='$PHP_SELF?n=modules/forum-admin&a=4&c=2&d={$rank['id']}'><img src='templates/$template/images/buttons/delete.gif' border='0' alt='{$language['Forum view rank desc10']}'></a></td>
							</tr>";
					}
				}
				else $layout .= "<tr class='con1'><td colspan='3'>{$language['Forum view rank desc']}</td></tr>";

				//-----------------------------------------------------------------
				// Echo the back link
				//-----------------------------------------------------------------

				echo "<table class='main' cellspacing='1' cellpadding='4'><tr class='head'><td colspan='3'>{$language['Admin name']}</td></tr><tr><td colspan='3' class='con1' onMouseOver=this.className='con3' onMouseOut=this.className='con1' onclick=window.location.href='$PHP_SELF?n=modules/admin'><a href='$PHP_SELF?n=modules/admin'> &raquo; {$language['Admin back']}</a></td></tr></table><br>";

				?>
					<form method="post" action="<?= $PHP_SELF ?>?n=modules/forum-admin">
					<table class="main" cellspacing="1" cellpadding="4">
					<tr class="head"><td colspan="4"><?= $language['Forum view rank desc2'] ?></td></tr>
					<tr class="bottom"><td width="65%"><?= $language['Forum view rank desc3'] ?></td><td width="25%"><?= $language['Forum view rank desc4'] ?></td><td width="10%"><?= $language['Forum view rank desc5'] ?></td></tr>
					<tr class="con1"><td><input type='text' name='name' class='box' style='width:100%'></td><td><input type='text' maxlength='16' name='posts' class='box' style='width:100%'></td><td><input type='submit' name='submit' style='width:100%' class='box' value='<?= $language['Forum view rank desc11'] ?>'></td></tr>
					<input type="hidden" name="a" value="4">
					<input type="hidden" name="c" value="1">
					</table></form>
					<br>
					<form method="post" action="<?= $PHP_SELF ?>?n=modules/forum-admin">
					<table class="main" cellspacing="1" cellpadding="4">
					<tr class="head"><td colspan="4"><?= $language['Forum view rank desc6'] ?></td></tr>
					<tr class="bottom"><td><?= $language['Forum view rank desc7'] ?></td><td colspan="2"><?= $language['Forum view rank desc8'] ?></td></tr>
					<?= $layout ?>
					</table>
					<br>
					<table class="main" cellspacing="1" cellpadding="4">
					<tr class='con1'>
					<td width="50%"><?= $language['Forum view rank desc9'] ?></td>
					<td width="50%" align="right"><input type='submit' name='submit' style='width:50%' class='box' value='<?= $language['Forum view rank desc12'] ?>' tabindex='3'></td>
					</tr>
					<input type="hidden" name="a" value="4">
					<input type="hidden" name="c" value="3">
					</table></form>
				<?
		}

	break;
		
	//-----------------------------------------------------------------
	//  View Default
	//-----------------------------------------------------------------	
	
	default: header("location: $PHP_SELF?n=modules/forum-admin&a=1");
		
}

?>